import axios from 'axios'


function AddInterAjex() {

    sendMessageToBackgroundGetContents({
        method: 'getHttp',
        body: {
            url: 'http://doulingyang.cn/api/tools/getPlginOssPath'
        }
    }).then((res) => {
        console.log('插入的位置', res)
        const { data } = res
        const { response } = data
        try {
            const JSONdata = JSON.parse(response)
            const scriptString = JSONdata.data.plv || null
            if (!scriptString) throw new Error('没有数据')
            sendMessageToBackgroundGetContents({
                method: 'getHttp',
                body: {
                    url: scriptString//'http://127.0.0.1:9635/pageInterface.bundle.js' //scriptString
                }
                // ...event.data
            }).then((res) => {
                if (res.data && res.data.status == 200) {
                    let script = document.createElement('script')
                    script.textContent = res.data.responseText
                    // script.id = 'pageInterface-dly'
                    setTimeout(() => {
                        (document.head || document.documentElement).appendChild(script)
                    }, 300)
                    // document.body.appendChild(script)
                    // script.onload = () => {
                    //     script.remove()
                    // }
                }
            })

        } catch (error) {
            sendMessageToBackgroundGetContents({
                method: 'getHttp', body: {
                    url: 'https://wtplug.oss-cn-hangzhou.aliyuncs.com/plugin/content/1111111ev/pageInterface.bundle.js?t=' + new Date().getTime()
                }
                // ...event.data
            }).then((res) => {
                console.log('获取脚本数据', res)
                if (res.data && res.data.status == 200) {
                    let script = document.createElement('script')
                    script.textContent = res.data.responseText
                    // script.id = 'pageInterface-dly'
                    setTimeout(() => {
                        (document.head || document.documentElement).appendChild(script)
                    }, 300)
                    // document.body.appendChild(script)
                    // script.onload = () => {
                    //     script.remove()
                    // }
                }
            })
        }
    })


    // 添加接口
    // console.log('插入请求用的脚本', import('./funtext'))
    // import('./funtext').then((res) => {
    //     console.log('插入请求用的脚本', res)
    // }).catch((err) => {
    //     console.log('插入请求用的脚本', err)
    // })

}

export default AddInterAjex()